package com.fduss.artStore.service.impl;

import com.fduss.artStore.dao.CustomerDao;
import com.fduss.artStore.dao.CustomerLogonDao;
import com.fduss.artStore.dao.impl.CustomerDaoImpl;
import com.fduss.artStore.dao.impl.CustomerLogonDaoImpl;
import com.fduss.artStore.entity.Customer;
import com.fduss.artStore.entity.CustomerLogon;
import com.fduss.artStore.service.CustomerService;

/**
 * @Author duocai wu
 * @Date 2017/6/24
 * @Time 14:40
 */
public class CustomerServiceImpl implements CustomerService {

    private CustomerDao customerDao;
    private CustomerLogonDao customerLogonDao;

    public CustomerServiceImpl() {
        customerDao = new CustomerDaoImpl();
        customerLogonDao = new CustomerLogonDaoImpl();
    }

    /**
     * 验证用户登录
     *
     * @param userName
     * @param pass
     * @return 登录成功返回顾客基本信息，否则返回null
     */
    @Override
    public Customer login(String userName, String pass) {
        CustomerLogon customerLogon = customerLogonDao.getOne(userName, pass);
        if (customerLogon != null) {
            Customer customer = customerDao.getOne(customerLogon.getCustomerId());
            customer.setCustomerLogon(customerLogon);
            return customer;
        }
        return null;
    }

    public void delete(String userName, String pass) {
        CustomerLogon customerLogon = customerLogonDao.getOne(userName, pass);
        if (customerLogon != null) {
            customerLogonDao.delete(customerLogon.getCustomerId());
            customerDao.delete(customerLogon.getCustomerId());
        }
    }

    /**
     * 验证用户注册
     *
     * @param userName
     * @param pass
     * @return 注册成功返回顾客基本信息，否则返回null
     */
    @Override
    public Customer register(String userName, String pass) {
        CustomerLogon customerLogon = customerLogonDao.add(new CustomerLogon(userName, pass));
        if (customerLogon != null) {
            Customer customer = customerDao.add(new Customer(customerLogon.getCustomerId()));
            customer.setCustomerLogon(customerLogon);
            return customer;
        }
        return null;
    }

    /**
     * 更改用户信息, 密码除外
     *
     * @param customer
     * @return 更改后的用户
     */
    @Override
    public Customer update(Customer customer) {
        return customerDao.update(customer);
    }

    @Override
    public boolean updateCustomerStatus(String userName, String pass, int newStatus) {
        CustomerLogon customerLogon = customerLogonDao.getOne(userName, pass);
        if (customerLogon != null) {
            customerLogon.setState(newStatus);
            return customerLogonDao.update(customerLogon) != null;
        }
        return false;
    }

    @Override
    public boolean updatePassword(String userName, String oldPass, String newPass) {
        CustomerLogon customerLogon = customerLogonDao.getOne(userName, oldPass);
        if (customerLogon != null) {
            return customerLogonDao.updatePassword(customerLogon.getCustomerId(), newPass);
        }
        return false;
    }
}
